<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: SiteMapPortlet.php 426 2011-01-05 10:36:10Z steen.rabol $
 *
 */

Prado::using('Application.Portlets.Portlet');
Prado::using('Application.Portlets.SiteMapPortlet.Common.SiteMapPortletRecord');
Prado::using('Application.Common.Data.PortalSiteMenuRecord');
Prado::using('Application.Common.Data.PortalSiteMenuItemRecord');

class SiteMapPortlet extends Portlet
{
	private	$ditem;
	private $imgfolder;

	public function onPreRender($param)
	{
		parent::onPreRender($param);

		// Publish assets
		$assetManager	= $this->Application->AssetManager;
		$clientScripts	= $this->Page->ClientScript;

		$jsurl = $assetManager->publishFilePath(dirname(__FILE__) . '/Assets');
//		$clientScripts->registerHeadScriptFile('sitemapportlet_assets', $jsurl . '/dtree.js');
//		$clientScripts->registerStyleSheetFile('sitemapportlet_assets_css', $jsurl . '/dtree.css');

		$clientScripts->registerHeadScriptFile('dtree_assets', $jsurl . '/dtree.js');
		$clientScripts->registerStyleSheetFile('dtree_assets_css', $jsurl . '/dtree.css');

		$imgfolder = $jsurl . '/';

		$script ="";
		$script .="d = new dTree('d','$imgfolder');";
		$script .="d.add(0,-1,'" . Prado::localize('Home') . "');";

		// Add nodes
		$sm	= PortalSiteMenuRecord::finder()->findByactive(1);

		if($sm && is_object($sm))
		{
			$criteria = new TActiveRecordCriteria;
			$criteria->Condition				= 'smid = :smid and parent=:parent and active=:active';
			$criteria->Parameters[':smid']		= $sm->id;
			$criteria->Parameters[':parent']	= 0;
			$criteria->Parameters[':active']	= 1;
			$criteria->OrdersBy['displayorder'] = 'asc';
			$menitemspages						= PortalSiteMenuItemRecord::finder()->findAll($criteria);

			if($menitemspages)
			{
				$this->ditem = 0;
				foreach($menitemspages as $mipage)
				{
					$this->ditem++;
					$script .="d.add(" . $this->ditem . ",0,'" . $mipage->title . "','" . $this->Service->constructUrl($mipage->page) . "','" . $mipage->title . "');";
					$script .= $this->AddSubItems($this->ditem,$sm->id,$mipage->id);
				}
			}
		}

		$script .="document.write(d);";
		$script .=" d.closeAll();";
		$this->dtSiteMap->Text = $script;
	}

	public function SearchQuery($queryvalue)
	{
		return null;
	}

	public function AddSubItems($parent,$mid, $pid)
	{
		$script = "";
		$criteria = new TActiveRecordCriteria;
		$criteria->Condition = 'smid = :smid and parent=:parent and active=:active';
		$criteria->Parameters[':smid']		= $mid;
		$criteria->Parameters[':parent']	= $pid;
		$criteria->Parameters[':active']	= 1;
		$criteria->OrdersBy['displayorder'] = 'asc';
		$menitemspages					= PortalSiteMenuItemRecord::finder()->findAll($criteria);

		if($menitemspages)
		{
			foreach($menitemspages as $mipage)
			{
				$this->ditem++;
				$script .= "d.add(" . $this->ditem . ",$parent,'" . $mipage->title . "','" . $this->Service->constructUrl($mipage->page) . "','" . $mipage->title . "');";
				$script .= $this->AddSubItems($this->ditem,$mid,$mipage->id);
			}
		}

		return $script;
	}
}
?>